<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DrumKit</title>
    <style>
        html {
            font-size: 10px;
            background: url(https://i.imgur.com/b9r5sEL.jpg) bottom center;
            background-size: cover;
        }

        body,
        html {
            margin: 0;
            padding: 0;
        }

        .keys {
            display: flex;
            min-height: 100vh;
            align-items: center;
            justify-content: space-around;
        }

        .key {
            border: .4rem solid black;
            border-radius: .2rem;
            color: white;
            width: 10rem;
            margin: 1rem;
            padding: .1rem .5rem;
            text-align: center;
        }

        .play {
            border-color: #ffc600;
            box-shadow: 0 0 1rem #ffc600;
        }
    </style>
</head>

<body>
    <div class="keys">
        <div class="key" data-key="65">A</div>
        <div class="key" data-key="83">S</div>
        <div class="key" data-key="68">D</div>
        <div class="key" data-key="70">F</div>
        <div class="key" data-key="71">G</div>
        <div class="key" data-key="72">H</div>
        <div class="key" data-key="74">J</div>
        <div class="key" data-key="75">K</div>
        <div class="key" data-key="76">L</div>
    </div>
    <audio src="./sounds/boom.wav" data-key="65"></audio>
    <audio src="./sounds/clap.wav" data-key="83"></audio>
    <audio src="./sounds/hihat.wav" data-key="68"></audio>
    <audio src="./sounds/kick.wav" data-key="70"></audio>
    <audio src="./sounds/openhat.wav" data-key="71"></audio>
    <audio src="./sounds/ride.wav" data-key="72"></audio>
    <audio src="./sounds/snare.wav" data-key="74"></audio>
    <audio src="./sounds/tink.wav" data-key="75"></audio>
    <audio src="./sounds/tom.wav" data-key="76"></audio>
    <script>
        /* window.addEventListener("keydown", (e) => {
            let key = document.querySelector(`div[data-key="${e.keyCode}"]`)
            let audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
            //console.log(key)
            if (key) {
                key.classList.add('play')
                audio.currentTime = 0
                audio.play()
            }
        })
        window.addEventListener("keyup", (e) => {
            let key = document.querySelector(`div[data-key="${e.keyCode}"]`)
            let audio = document.querySelector(`audio[data-key="${e.keyCode}"]`)
            //console.log(key)
            if (key) {
                key.classList.remove('play')
                audio.currentTime = 0
            }

        })
        let keys = document.querySelectorAll(".key")
        keys.forEach((key) => {
            key.addEventListener('mousedown', (e) => {
                e.target.classList.add('play')
            })
        })
        keys.forEach((key) => {
            key.addEventListener('mouseup', (e) => {
                e.target.classList.remove('play')

            })
        }) */
        function musicPlay(e) {
            let keyCode = e.keyCode || e.target.getAttribute('data-key')
            let key = document.querySelector(`div[data-key="${keyCode}"]`)
            let audio = document.querySelector(`audio[data-key="${keyCode}"]`)
            if (key) {
                key.classList.add('play')
                audio.currentTime = 0
                audio.play()
            }
        }

        function musicPause(e) {
            let keyCode = e.keyCode || e.target.getAttribute('data-key')
            let key = document.querySelector(`div[data-key="${keyCode}"]`)
            let audio = document.querySelector(`audio[data-key="${keyCode}"]`)
            if (key) {
                key.classList.remove('play')
                audio.currentTime = 0
            }
        }
        window.addEventListener('keydown', musicPlay)
        window.addEventListener('keyup', musicPause)
        let keys = document.querySelectorAll(".key")
        keys.forEach((key) => {
            key.addEventListener('mousedown', musicPlay)
        })
        keys.forEach((key) => {
            key.addEventListener('mouseup', musicPause)
        })
    </script>
</body>

</html>